package com.weiyuwqj.admin.controller.common;


import com.weiyuwqj.api.tool.QiniuUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Copyright 2015-2019 Yunfucloud technology co., Ltd.
 * Created by fuxx on 15/12/22.
 */
@Controller
public class UploadController {

    @ResponseBody
    @RequestMapping(value = "/umeditorFile", method = RequestMethod.POST)
    public Object umeditorFile(@RequestParam("file") MultipartFile file,HttpServletRequest request) {

        String serverDir=request.getServletContext().getRealPath("/");

        Object ret=fileUploadHandle(file,serverDir,"/um");
        return ret;
    }

    @ResponseBody
    @RequestMapping(value = "/itemFile", method = RequestMethod.POST)
    public Object itemFile(@RequestParam("file") MultipartFile file,HttpServletRequest request) {

        String serverDir=request.getServletContext().getRealPath("/");

        Object ret=fileUploadHandle(file,serverDir,"/item");
        return ret;
    }

    @ResponseBody
    @RequestMapping(value = "/articleFile", method = RequestMethod.POST)
    public Object articleFile(@RequestParam("file") MultipartFile file,HttpServletRequest request) {

        String serverDir=request.getServletContext().getRealPath("/");

        Object ret=fileUploadHandle(file,serverDir,"/article");
        return ret;
    }

    @ResponseBody
    @RequestMapping(value = "/pictureFile", method = RequestMethod.POST)
    public Object pictureFile(@RequestParam("file") MultipartFile file,HttpServletRequest request) {

        String serverDir=request.getServletContext().getRealPath("/");

        Object ret=fileUploadHandle(file,serverDir,"/picture");
        return ret;
    }

    /*private Object htmlFileUploadHandle(MultipartFile file,String serverDir,String targetDir){
        HashMap<String,Object> ret=new HashMap<String, Object>();
        File dir=new File(serverDir+"upload");
        String fileName=file.getOriginalFilename();
        if(!dir.exists()){
            dir.mkdir();
        }
        String fullPath="";
        String newFileName=new Date().getTime()+".pdf";
        if(targetDir!=null&&targetDir.isEmpty()){
            newFileName=targetDir+"/"+newFileName;
        }
        if (!file.isEmpty()) {
            try {
                fullPath= serverDir+"upload\\"+ newFileName;
                //拿到输出流，同时重命名上传的文件
                HtmlTranslator.html2pdf(file,fullPath);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("上传出错");
            }
            String url=QiniuUpload.uploadQiniu(fullPath,newFileName);
            ret.put("state","SUCCESS");
            ret.put("url",url);

        } else {
            ret.put("state","error");
        }
        ret.put("statusText","sdfsdfds");
        return ret;
    }*/

    private Object fileUploadHandle(MultipartFile file,String serverDir,String targetDir){
        HashMap<String,Object> ret=new HashMap<String, Object>();
        File dir=new File(serverDir+"upload");
        String fileName=file.getOriginalFilename();
        String ext=fileName.substring(fileName.lastIndexOf("."));
        String url="/upload/"+new Date().getTime()+ext;
        if(!dir.exists()){
            dir.mkdir();
        }
        String fullPath="";
        String newFileName=new Date().getTime()+ext;
        if(targetDir!=null&&targetDir.isEmpty()){
            newFileName=targetDir+"/"+newFileName;
        }
        if (!file.isEmpty()) {
            try {
                fullPath= serverDir+"upload/"+ newFileName;
                //拿到输出流，同时重命名上传的文件
                FileOutputStream os = new FileOutputStream(fullPath);
                //拿到上传文件的输入流
                InputStream in = (InputStream) file.getInputStream();
                //以写字节的方式写文件
                int b = 0;
                while((b=in.read()) != -1){
                    os.write(b);
                }
                os.flush();
                os.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("上传出错");
            }

            url= QiniuUpload.uploadQiniu(fullPath, newFileName);
            ret.put("state","SUCCESS");
            ret.put("url",url);

        } else {
            ret.put("state","error");
        }
        ret.put("statusText","sdfsdfds");
        return ret;
    }

}
